package com.example.study_manger.control;


import com.example.study_manger.entity.Exam;
import com.example.study_manger.entity.Organization;
import com.example.study_manger.entity.TrainingPlan;
import com.example.study_manger.repository.ExamRe;
import com.example.study_manger.repository.OrganizationRe;
import com.example.study_manger.repository.TrainingPlanRe;
import com.example.study_manger.repository.UserTbRe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class TestController {
    @Autowired
    private ExamRe examRe;
    @Autowired
    private UserTbRe userTbRe;
    @Autowired
    private TrainingPlanRe trainingPlanRe;
    @Autowired
    private OrganizationRe organizationRe;
    @PostMapping("/getTest/{id}")
    public List<Exam> getTest(@PathVariable Long id){
        System.out.println(id);
        System.out.println(examRe.findByTrainPlanId(id));
        return examRe.findByTrainPlanId(id);
    }
    @PostMapping("/getTestList")
    public List<Exam> getTestList(@RequestBody String username){
        System.out.println("getTestList:"+username);
        Organization aPublic = organizationRe.findByName("public");
        return examRe.findAllByOrganization(aPublic);

    }
    @PostMapping("addTest/{id}")
    public List<Exam> addTest(@PathVariable Long id,@RequestBody Exam exam){
        System.out.println(id);
        System.out.println(exam);
        TrainingPlan plan=trainingPlanRe.findById(id).get();
        exam.setTrainingPlan(plan);
        examRe.save(exam);
        return examRe.findByTrainPlanId(id);
    }
    @PostMapping("deleteTest/{id}")
    public List<Exam> deleteTest(@PathVariable Long id){
        Exam exam=examRe.findById(id).get();
        Long planId=exam.getTrainingPlan().getId();
        exam.setTrainingPlan(null);
        examRe.save(exam);
        return examRe.findByTrainPlanId(planId);
    }

}
